package com.durian;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.alibaba.android.arouter.launcher.ARouter;
import com.durian.app.lifecycle.annotation.AppLifeCycle;
import com.durian.app.lifecycle.api.BaseAppLike;
import com.durian.base.frame.ActivityStack;
import com.durian.base.frame.IActivityStack;
import com.durian.base.net.HttpConfig;
import com.durian.base.utils.ToolUtils;

/**
 * 说明：BaseApp
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-06-14 18:27
 * <p/>
 * 版本：version 1.0
 */
@AppLifeCycle
public class BaseApp extends BaseAppLike {

    private static boolean isAppDebug = false;//是否是调试模式

    private static Application mApplication;
    private static Handler     sHandler  = new Handler(Looper.getMainLooper());

    public static Handler handler(){
        return sHandler;
    }

    /**
     * Be sure execute in main thread.
     *
     * @param runnable code
     */
    public static void runInMainThread(Runnable runnable) {
        if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
            if (sHandler != null){
                sHandler.post(runnable);
            }
        } else {
            runnable.run();
        }
    }

    /**
     * Be sure execute in main thread.
     *
     * @param runnable code
     */
    public static void runInMainThreadDelayed(long delayed,Runnable runnable) {
        if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
            if (sHandler != null){
                sHandler.postDelayed(runnable,delayed);
            }
        } else {
            runnable.run();
        }
    }

    public static void setDebug(boolean isAppDebug){
        BaseApp.isAppDebug = isAppDebug;
    }

    public static Application getApp() {
        return mApplication;
    }

    @Override
    public int getPriority() {
        return 100;
    }

    @Override
    public void onCreate(Application application) {
        mApplication = application;
        initARouter(application);
        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

            private boolean isSkip(Activity activity){
                if (activity == null || ToolUtils.isTranslucentActivity(activity) || !(activity instanceof IActivityStack)){
                    return true;
                }else {
                    return !((IActivityStack) activity).addActivityStack();
                }
            }

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                if (!isSkip(activity)){
                    ActivityStack.create().addActivity(activity);
                }
            }

            @Override
            public void onActivityStarted(Activity activity) {
                if (!isSkip(activity)){
                    ActivityStack.create().setCurrentActivity(activity);
                }
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                if (!isSkip(activity)){
                    ActivityStack.create().setCurrentActivity(null);
                }
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                if (!isSkip(activity)){
                    ActivityStack.create().removeActivity(activity);
                }
            }
        });
    }

    @Override
    public void onTerminate() {

    }

    @Override
    public void onLowMemory() {

    }

    /**
     * app是否上线
     * @return
     */
    public static boolean isAppDebug(){
        return isAppDebug;
    }

    /**
     * 初始ARouter
     * @param application
     */
    private void initARouter(Application application){
        if (isAppDebug()){
            ARouter.openDebug();
            ARouter.openLog();
        }
        HttpConfig.get().setDebug(isAppDebug());
        ARouter.init(application);
    }

}
